//
//  TestJSCController.m
//  testApp
//
//  Created by iMac on 2018/2/25.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "TestJSCController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface TestJSCController ()

@end

@implementation TestJSCController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self testFunction];
    [self addDismissButton];
}



#pragma mark 测试  在JavaScript中调用native代码
- (void)testFunction {
    JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
    JSContext *context = [[JSContext alloc] initWithVirtualMachine:vm];
    context[@"add"] = ^(NSInteger a, NSInteger b) {
        return a+b;
    };
    JSValue *resultValue = [context evaluateScript:@"add(5, 6)"];
    NSLog(@"resultValue = %@", resultValue);
    resultValue = [context[@"add"] callWithArguments:@[@5, @6]];
    NSLog(@"resultValue = %@", resultValue);
}





#pragma mark OC与JS数组转换
- (void)testArrayTransfrom {
    JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
    JSContext *context = [[JSContext alloc] initWithVirtualMachine:vm];
    [context evaluateScript:@"var friends = ['Alice', 'Jenny', 'XiaoMing']"];
    JSValue *friendsValue = context[@"friends"];
    NSLog(@"%@, %@, %@", friendsValue[0], friendsValue[1], friendsValue[2]);
    NSArray *friendsArr = [friendsValue toArray];
    NSLog(@"%@, %@, %@", friendsArr[0], friendsArr[1],friendsArr[2]);
    
    //native->js 我觉XiaoMing和不不错，给你再推荐个Jimmy
    context[@"girlFriends"] = @[friendsArr[2], @"Jimmy"];
    [context evaluateScript:@"log('girlFriends :'+girlFriends[0]+' '+girlFriends[1])"];
}


#pragma mark NSDictionary 与 JS 对象
- (void)testDictAndJS {
    JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
    JSContext *context = [[JSContext alloc] initWithVirtualMachine:vm];
    [context evaluateScript:@"var color = {red:230, green:90, blue:100}"];
    JSValue *colorValue = context[@"color"];
    NSLog(@"这是, 从colorValue里边取值 \n r=%@, g=%@, b=%@", colorValue[@"red"], colorValue[@"green"], colorValue[@"blue"]);
    NSDictionary *colorDic = [colorValue toDictionary];
    NSLog(@"这是, 转换为dic后, 从dic中取值 \n r=%@, g=%@, b=%@", colorDic[@"red"], colorDic[@"green"], colorDic[@"blue"]);
    
    context[@"color"] = @{@"red" : @0,
                          @"green" : @0,
                          @"blue" : @0
                          };
    [context evaluateScript:@"log('r: ' + color.red + 'g: ' + color.green + 'b: ' + color.blue)"];
}


#pragma mark JSContext
    // 一个JSContext对象代表一个JavaScript执行环境。在native代码中，使用JSContext去执行JS代码，访问JS中定义或者计算的值，并使JavaScript可以访问native的对象、方法、函数。
/*  -- JSContext访问js对象
 JSValue *value = [context evaluateScript:@"var a = 1+2*3;"];
 NSLog(@"a = %@", [context objectForKeyedSubscript:@"a"]);
 NSLog(@"a = %@", [context.globalObject objectForKeyedSubscript:@"a"]);
 NSLog(@"a = %@", context[@"a"]);
 */


#pragma mark 线程和JavaScript的并发执行
    //JavaScriptCore API都是线程安全的。你可以在任意线程创建JSValue或者执行JS代码，然而，所有其他想要使用该虚拟机的线程都要等待。
    // 如果想并发执行JS，需要使用多个不同的虚拟机来实现。
    // 可以在子线程中执行JS代码。
- (void)testSquence {

}

#pragma mark JavaScriptCore 的 helloworld 代码
- (void)helloWorld {
    // 创建虚拟机
    JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
    // 创建上下文
    JSContext *context = [[JSContext alloc] initWithVirtualMachine:vm];
    // 执行JavaScript代码并获取返回值
    JSValue *value = [context evaluateScript:@"1+2*3"];
    // 转换成OC数据并打印
    NSLog(@"value = %d", [value toInt32]);
}



- (void)addDismissButton {
    UIButton *dismiss = ({
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"dismiss" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CGFloat f = 100;
        btn.frame = CGRectMake(SCREEN_WIDTH - f, SCREEN_HEIGHT - f, f, f);
        [btn setBackgroundColor:[UIColor purpleColor]];
        [btn addTarget:self action:@selector(dismissButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self.view addSubview:dismiss];
}
- (void)dismissButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:nil];
}

@end
